home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Essentials / AppleScanGS / Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-06  |  13.2 KB  |  497 lines  |  [TEXT/MPS ]

  1. /***********************************************************************
  2. *
  3. * Window.c
  4. *
  5. * Copyright (c)  1990
  6. * Apple Computer, Inc.
  7. * All Rights Reserved.
  8. *
  9. * This file contains the code which implements  
  10. * windows in the scanner program.
  11. *
  12. ***********************************************************************/
  13.  
  14. #include <intmath.h>
  15. #include <memory.h>
  16. #include <types.h>
  17. #include <quickdraw.h>
  18. #include <qdaux.h>
  19. #include <resources.h>
  20. #include <window.h>
  21. #include <misctool.h>
  22. #include <control.h>
  23. #include <menu.h>
  24. #include <malloc.h>
  25.  
  26. #include "Scan.h"
  27.  
  28. #define xRulerInset 10
  29. #define yRulerInset 10
  30.  
  31. extern scannerWindowDef    sWindowDef;
  32. extern unsigned int     staggerCount;
  33. extern PicHndl            previewRuler;
  34.  
  35. void updatePreviewWindow();
  36.  
  37.  
  38. extern GrafPortPtr        previewWindowPtr;
  39. extern GrafPortPtr        settingsWindowPtr;
  40.  
  41. /* grayscale pallette */
  42. int cTable[16]= {0x000,0x111,0x222,0x333,0x444,0x555,0x666,0x777,
  43.                  0x888,0x999,0xAAA,0xBBB,0xCCC,0xDDD,0xEEE,0xFFF};
  44.  
  45. /* standard pallette */
  46. int defCTable[16] = {0x000,0x777,0x841,0x72C,0x00F,0x080,0xF70,0xD00,
  47.                      0xFA9,0xFF0,0x0E0,0x4DF,0xDAF,0x78F,0xCCC,0xFFF};
  48.  
  49. /*************************************************************************************************/
  50.  
  51. static CtlRecHndl makeBoxCtl(theWindow)
  52. GrafPortPtr    theWindow;
  53. {
  54.     CtlRecHndl    boxCtl;
  55.     static Rect    boxRect = { YINSET,XINSET,YINSET+(11*vPPI)+1,XINSET+((85*hPPI)/10)+1 };
  56.     static int    boxData[] = {    YINSET,                    /* min Y */
  57.                                 XINSET,                    /* min X */
  58.                                 YINSET+(14*vPPI)+1,        /* max Y */
  59.                                 XINSET+((85*hPPI)/10)+1,/* max X */
  60.                                 10,                        /* minimum height */
  61.                                 16,                        /* minimum width */
  62.                                 1,                        /* grid Y */
  63.                                 1 };                    /* grid X */
  64.     static int    boxColorTbl = 0x00B0;
  65.     extern void BoxProc();
  66.  
  67.     boxCtl = NewControl(theWindow,&boxRect,NULL,0x0006,0x0303,boxData,BoxProc,NULL,&boxColorTbl);
  68. /*                        |          |           |    |       |      |          |          |       ColorTable
  69.                         |          |           |    |       |      |          |          RefCon
  70.                         |          |           |    |       |      |          DefProc
  71.                         |          |           |    |       |      custom data
  72.                         |          |           |    |       width/heighth of knobs
  73.                         |          |           |    ctlFlag
  74.                         |          |           title pointer
  75.                         |          control rectangle
  76.                         window pointer    */
  77.     if(_toolErr)
  78.         ErrorWindow(0,NULL,_toolErr);
  79.     return(boxCtl);
  80. }
  81.  
  82. openPreview()
  83. {
  84. #define    bufferSize    968L
  85.  
  86.     Handle        tempHandle, bufferHandle;
  87.     LocInfoPtr    theLocInfo;
  88.     GrafPortPtr    theWindow;
  89.     Rect        *theRect;
  90.     int            fontWidth, rulerDepth1,rulerDepth2;
  91.     unsigned    i;
  92.  
  93.     if(previewWindowPtr != NULL) {
  94.         SelectWindow(previewWindowPtr);
  95.     } else {
  96.         bufferHandle = NewHandle(bufferSize,_ownerid,attrFixed,NULL); 
  97.         if(_toolErr != 0) {
  98.                 AlertWindow(4,NULL,(long) noMemAvailable);
  99.         } else {
  100.              tempHandle = NewHandle((long)sizeof(LocInfo),_ownerid,attrFixed,NULL);
  101.             theLocInfo = (LocInfoPtr)*tempHandle;
  102.             theLocInfo->portSCB = (sWindowDef.bitsPerPixel == 4) ? 0x80 : 0;
  103.             theLocInfo->ptrToPixImage = *bufferHandle;
  104.             theLocInfo->width = (104 * sWindowDef.bitsPerPixel + 7)/8;
  105.             theLocInfo->boundsRect.v1 = 0;
  106.             theLocInfo->boundsRect.h1 = 0;
  107.             theLocInfo->boundsRect.v2 = 149;
  108.             theLocInfo->boundsRect.h2 = 104;
  109.         
  110.             theWindow = NewWindow2(NULL,theLocInfo,updatePreviewWindow,NULL,2,PreviewWindow,rWindParam1);
  111.             SetPort(theWindow);
  112.  
  113.             previewWindowPtr = theWindow;
  114.     
  115.             SetDataSize(180,120,theWindow);
  116.  
  117.             makeBoxCtl(theWindow);    /* go add the custom control */
  118.  
  119.             if((previewRuler == NULL)) {
  120.  
  121.                 fontWidth = 8;
  122.  
  123.                 SetOrigin(0,0);
  124.                 GetPortRect(&theRect);
  125.                 rulerDepth1 = fontWidth+7;
  126.                 rulerDepth2 = (2*fontWidth)+7;
  127.  
  128.                     previewRuler = OpenPicture(&theRect);
  129.                     MoveTo(XINSET-XOFFSET,YINSET);
  130.  
  131.                 /* draw the vertical ruler first */
  132.                 /* draw hash marks */
  133.                     for(i = 0; i < 14; ++i) {
  134.                         Move(0,vPPI>>1);    /* move to first hash mark */
  135.                         Line(-3,0);    /* draw half-inch mark */
  136.                         Move(-4,vPPI>>1);    /* move to full hash mark */
  137.                         Line(7,0);    /* draw full-inch mark */
  138.                     }
  139.                     Move(0,-((vPPI*14)-4));        /* move back to top of ruler + 4 to center numerals */
  140.  
  141.                 /* mark the ruler divisions */
  142.                     for(i = 2; i < 10; i += 2) {
  143.                         Move(-rulerDepth1,vPPI<<1);
  144.                         DrawChar('0'+i);
  145.                         Move(7,0);
  146.                     }
  147.                     for(i = 10; i < 14; i += 2) {
  148.                         Move(-rulerDepth2,vPPI<<1);
  149.                         DrawChar('1');
  150.                         DrawChar('0'+(i-10));
  151.                         Move(7,0);
  152.                     }
  153.                     Move(0,(vPPI*2)-4);    /* position pen to bottom right corner */
  154.  
  155.                 /* then draw the ruler rectangle */
  156.                     Line(-27,0);    /* bottom */
  157.                     Line(0,-(vPPI*14));    /* left side */
  158.                     Line(27,0);        /* top */
  159.                     Line(0,(vPPI*14));    /* right side of ruler box */
  160.  
  161.                 /* now draw the ruler along the top */
  162.                     Move(XOFFSET,-(vPPI*14+YOFFSET));    /* vertical ruler stopped at bottom right corner */
  163.                 
  164.                 /* draw the hash marks */
  165.                     for(i = 0; i < 8; ++i) {
  166.                         Move(hPPI>>1,0);
  167.                         Line(0,-2);
  168.                         Move(hPPI>>1,-2);
  169.                         Line(0,4);
  170.                     }
  171.                     Move(hPPI>>1,0);    /* the 8 1/2 mark */
  172.                     Line(0,-2);
  173.                     Move(-((hPPI*8)+(hPPI>>1)-(fontWidth>>1)),2);    /* reposition back to start of ruler */
  174.                 
  175.                 /* draw the ruler divisions */
  176.                     for(i = 2; i < 10; i += 2) {
  177.                         Move((hPPI<<1)-fontWidth,-5);
  178.                         DrawChar('0'+i);
  179.                         Move(0,5);
  180.                     }
  181.                     Move((hPPI>>1)-(fontWidth>>1),0);    /* move to lower-right corner of ruler */
  182.                 
  183.                 /* draw the ruler frame */
  184.                     Line(0,-14);
  185.                     Line(-((hPPI*8)+(hPPI>>1)),0);
  186.                     Line(0,14);
  187.                     Line(((hPPI*8)+(hPPI>>1)),0);
  188.                     
  189.                 ClosePicture(); 
  190.             }
  191.             EnableMItem(CloseID);
  192.         }
  193.     }
  194. }
  195.  
  196. #if 0    /* is this used? */
  197. /***********************************************************************/
  198. void drawMousePos(){
  199.  
  200.     MouseRec    thisMouseRec;
  201.     CtlRecHndl    ctlHandle;
  202.     GrafPortPtr thisPort;
  203.     Pointer        theXArrayList[1];
  204.     Pointer        theYArrayList[1];
  205.     static char    xString[] = {"0000"};
  206.     static char    yString[] = {"0000"};
  207.     
  208.     theXArrayList[0] = xString;
  209.     theYArrayList[0] = yString;
  210.     
  211.     if(settingsWindowPtr != 0){
  212.         thisMouseRec = ReadMouse();
  213.     /*     thisPort = GetPort();
  214.         SetPort(settingsWindowPtr); */
  215.     
  216.     /*    
  217.         ctlHandle = GetCtlHandleFromID(settingsWindowPtr,WinXEndText);
  218.         Int2Dec(thisMouseRec.xPos,xString,4,FALSE);
  219.         SetCtlParamPtr(theXArrayList);
  220.         DrawOneCtl(ctlHandle);
  221.     
  222.         ctlHandle = GetCtlHandleFromID(settingsWindowPtr,WinYEndText);
  223.         Int2Dec(thisMouseRec.yPos,yString,4,FALSE);
  224.         SetCtlParamPtr(theYArrayList);
  225.         DrawOneCtl(ctlHandle);
  226.     */
  227.     /*    SetPort(thisPort); */
  228.     }
  229.     
  230. }
  231.  
  232. /***********************************************************************
  233. *
  234. * doInPreview
  235. *
  236. * This routine draws the contents of all the windows.
  237. *
  238. ***********************************************************************/
  239. void    doInPreview(mouseStart)
  240. Point    mouseStart;
  241. {
  242.     CtlRecHndl    ctlHandle;
  243.     Pointer        theXArrayList[1];
  244.     Pointer        theYArrayList[1];
  245.     static char    xString[] = {"0000"};
  246.     static char    yString[] = {"0000"};
  247.     Rect        limitRect,slopRect;
  248.     Point        mouseEnd;
  249.     word        dragFlag, penMode;
  250.     Long        changePos;
  251.     static char    test[32]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  252.                     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
  253.     static char    penMaskUnDraw[32]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  254.                     0x00,0x00,0x00,0x00f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  255.  
  256.     
  257.     theXArrayList[0] = xString;
  258.     theYArrayList[0] = yString;
  259.  
  260.     penMode = GetPenMode();
  261. /*    SetPenMode(modeXOR);
  262.     SetPenMask( */
  263.     if(((dragRect.v1)|(dragRect.h1)|(dragRect.v2)|(dragRect.h2))) {
  264.         FrameRect(&dragRect);
  265.     }
  266.     GetPortRect(&slopRect);
  267.     limitRect.v1 = slopRect.v1 + YINSET-1;
  268.     limitRect.h1 = slopRect.h1 + XINSET-4;
  269.     limitRect.v2 = slopRect.v2 -5;
  270.     limitRect.h2 = slopRect.h2 -109;
  271.     GlobalToLocal(&mouseStart);
  272.     
  273.     if(mouseStart.h < limitRect.h1) mouseStart.h = limitRect.h1;
  274.     if(mouseStart.v < limitRect.v1) mouseStart.v = limitRect.v1;
  275.     if(mouseStart.h > limitRect.h2) mouseStart.h = limitRect.h2;
  276.     if(mouseStart.v > limitRect.v2) mouseStart.v = limitRect.v2;
  277.     
  278. /*    ctlHandle = GetCtlHandleFromID(settingsWindowPtr,WinYStartText);
  279.     Int2Dec(mouseStart.v,yString,4,FALSE);
  280.     SetCtlParamPtr(theYArrayList);
  281.     DrawOneCtl(ctlHandle);
  282.  
  283.     ctlHandle = GetCtlHandleFromID(settingsWindowPtr,WinXStartText);
  284.     Int2Dec(mouseStart.h,xString,4,FALSE);
  285.     SetCtlParamPtr(theXArrayList);
  286.     DrawOneCtl(ctlHandle);
  287. */
  288.     
  289.     SetRect(&dragRect,mouseStart,mouseStart.h+2,mouseStart.v+2);
  290.     dragFlag = 0x0424; /* minimum of 4 pixels, return the final rect, drag rubberband rectangle */
  291.     
  292.     DragRect(drawMousePos,test,mouseStart.h,mouseStart.v,&dragRect,&limitRect,&slopRect,dragFlag);        
  293.  
  294.     FrameRect(&dragRect);
  295.     SetPenMode(penMode);    
  296. }
  297. #endif
  298.  
  299.  
  300. /***********************************************************************
  301. *
  302. * drawThisWindow
  303. *
  304. * This routine draws the contents of all the windows.
  305. *
  306. ***********************************************************************/
  307. void    drawThisWindow()
  308. {
  309.     DrawControls(GetPort());
  310. }
  311.  
  312.  
  313.     
  314. /***********************************************************************
  315. *
  316. * doCloseTop
  317. *
  318. * This routine closes the topmost window.  We do a little work to
  319. * prevent the main window from being closed.
  320. *
  321. ***********************************************************************/
  322. void    doCloseTop()
  323. {
  324.     GrafPortPtr     tempWin;
  325.     LocInfoPtr        theLocInfo;
  326.     char            **nameHndl;
  327.     unsigned int    k;
  328.  
  329.     tempWin = FrontWindow();
  330.  
  331.     if(tempWin == previewWindowPtr) {
  332.         previewWindowPtr = NULL;
  333.     }
  334.  
  335.     if(tempWin == settingsWindowPtr) {
  336.         settingsWindowPtr = NULL;
  337.     }
  338.         
  339.     if(tempWin != NULL) { 
  340.         if (theLocInfo = (LocInfoPtr)GetWRefCon(tempWin)) {
  341.             DisposeHandle(FindHandle(theLocInfo->ptrToPixImage));
  342.             DisposeHandle(FindHandle(theLocInfo));
  343.         }
  344.         nameHndl = (char **) GetWTitle(tempWin);    /* was the name allocated by us? */
  345.         if(!((long) nameHndl & 0x80000000))    /* not if it's a handle */
  346.             free(nameHndl);                /* else dipose of it */
  347.         CloseWindow(tempWin);
  348.     }
  349.     
  350.     if(!FrontWindow())
  351.         DisableMItem(CloseID);
  352.     
  353. }
  354.  
  355.  
  356.  
  357.  
  358. /***********************************************************************
  359. *
  360. * openThisWindow
  361. *
  362. * This routine either opens the specified window or brings it to the top
  363. * if it is already open.
  364. *
  365. * If it is not open, we open it with NewWindow2 invisibly, adjust the window's
  366. * location and then show and select the window.
  367. *
  368. ***********************************************************************/
  369. GrafPortPtr    openThisWindow(ctlid)
  370. unsigned long    ctlid;
  371. {
  372.     GrafPortPtr     wptr;
  373.  
  374.     wptr = NewWindow2(NULL, NULL, drawThisWindow, NULL, 2, ctlid, rWindParam1);
  375.     if (ctlid < AppWindow) {
  376.         MoveWindow (50 + 8 * staggerCount, 50 + 8 * staggerCount, wptr);
  377.         staggerCount++;
  378.         staggerCount &= 0x0F;
  379.     }
  380.     SelectWindow(wptr);
  381.  
  382.     return(wptr);
  383. }
  384.      
  385. /***********************************************************************
  386. *
  387. * setupWindows - initializes window stuff prior to entering event loop
  388. *
  389. ***********************************************************************/
  390. void    setupWindows()
  391. {
  392.     extern void doSettingsItem();
  393.     
  394.     doSettingsItem();
  395.     return;
  396. }
  397.  
  398. /***********************************************************************
  399. *
  400. * updateScannerWindow()
  401. *
  402. * updates the bit mapped windows with the scanned data.
  403. *
  404. ***********************************************************************/
  405. void    updateScannerWindow()
  406. {
  407.     LocInfoPtr theSrcLoc = (LocInfoPtr)GetWRefCon(GetPort());
  408.     PPToPort(theSrcLoc,&theSrcLoc->boundsRect,0,0,notCopy);
  409.     if (_toolErr)
  410.         ErrorWindow(0,NULL, _toolErr);
  411. }
  412. /***********************************************************************
  413. *
  414. * updatePreviewWindow()
  415. *
  416. * updates the bit mapped windows with the scanned data.
  417. *
  418. ***********************************************************************/
  419. void    updatePreviewWindow()
  420. {
  421.     Rect             theRect, pageRect;
  422.     
  423.     LocInfoPtr theSrcLoc = (LocInfoPtr)GetWRefCon(GetPort());
  424.     
  425.     GetPortRect(&theRect);    
  426.     DrawPicture(previewRuler,&theRect);
  427.  
  428. /*
  429.     if((dragRect.h1|dragRect.h2|dragRect.v1|dragRect.v2) != 0) {
  430.         FrameRect(&dragRect);
  431.     }
  432. */
  433.         
  434.     pageRect.v1 = YINSET-1;
  435.     pageRect.v2 = YINSET + (14 * vPPI) + 2;
  436.     pageRect.h1 = XINSET-1;
  437.     pageRect.h2 = XINSET + (8 * hPPI) + (hPPI >> 1) + 2;
  438.     SetSolidPenPat(7);
  439.     FrameRect(&pageRect);
  440.     if (_toolErr)
  441.         ErrorWindow(0,NULL,_toolErr);
  442.  
  443.     SetSolidPenPat(0);
  444.  
  445.     DrawControls(previewWindowPtr);
  446. }
  447.  
  448.  
  449.  
  450. /***********************************************************************
  451. *
  452. * doOpenImage
  453. *
  454. *
  455. ***********************************************************************/
  456. void doOpenImage(dataWidth, dataLength, bufferHandle)
  457. unsigned long     dataWidth, dataLength;
  458. Handle             bufferHandle;
  459. {
  460.     char            *windowName;
  461.     LocInfoPtr         theLocInfo;
  462.     GrafPortPtr        theWindow;
  463.     Handle             tempHandle;
  464.     int             mode;
  465.     int                pixelWidth;
  466.     static char windNum = '1';
  467.  
  468.     /* Check the screen mode and multiply by 2 if 640 */
  469.  
  470.     mode = (GetMasterSCB() & 0x0080) ? 1 : 2;
  471.  
  472.     pixelWidth = (dataWidth * 8) >> mode;
  473.  
  474.     tempHandle = NewHandle((long)sizeof(LocInfo),_ownerid,attrFixed,NULL);
  475.     theLocInfo = (LocInfoPtr)*tempHandle;
  476.     theLocInfo->portSCB = (mode == 2) ? 0x00 : 0x80;
  477.     theLocInfo->ptrToPixImage = *bufferHandle;
  478.     theLocInfo->width = dataWidth;
  479.     theLocInfo->boundsRect.v1 = 0;
  480.     theLocInfo->boundsRect.h1 = 0;
  481.     theLocInfo->boundsRect.v2 = dataLength;
  482.     theLocInfo->boundsRect.h2 = pixelWidth;
  483.     
  484.     windowName = malloc(12);    /* get memory for window title */
  485.     memcpy(windowName,"\p Untitled  ",12);
  486.     windowName[10] = windNum;
  487.     if(++windNum > '9')            /* adjust window number for next time through */
  488.         windNum = '1';
  489.     theWindow = NewWindow2(windowName,theLocInfo,updateScannerWindow,NULL,2,ImageWindow,rWindParam1);
  490.     SetPort(theWindow);
  491.  
  492.     SetDataSize(pixelWidth,(short)dataLength,theWindow);
  493.     
  494.     EnableMItem(CloseID);
  495.     free(windowName);
  496. }
  497.